Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zerr

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zerr

custom error construction

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ZErr

Custom JS error creator that subclasses Error, sets .name .stack and .message, and provides simple interpolation to construct error messages.

var zerr = require('zerr')

// first param: error name
// second param: string template for error messages (substitutes '%' with args in constructor)
var BadParam = zerr('BadParam', '% must be a %')

// using the created error:
try { throw new BadParam('foo', 'string') }
catch (e) {
  console.log(e.name) // => 'BadParamError'
  console.log(e.message) // => 'foo must be a string'
  console.log(e.stack) // => 'BadParamError: foo must be a string\nat repl:1:13...'
  console.log(e instanceof Error) // => true
  console.log(e instanceof BadParam) // => true
}

// the `new` is optional
throw BadParam('foo', 'string')

// if no string template is given, zerr will just use the constructor's param as the message
var BadParam = zerr('BadParam')
try { throw new BadParam('foo is bad') }
catch (e) {
  console.log(e.message) // => 'foo is bad'
}

// if you pass an error as the first param to the constructor, zerr will add its stack to the stack history
// (taken from https://github.com/dominictarr/explain-error)
try { throw new BadParam(new BadParam('earlier foo was bad'), 'foo is bad') }
catch (e) {
  console.log(e.stack) /* =>
  BadPararmError: foo is bad
      at repl:1:7
    BadPararmError: earlier foo was bad
      at repl:1:15
  */
}

Keywords

FAQs

Package last updated on 27 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc